﻿Imports System.Web.UI
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.Configuration
Imports System.Net.Mail

Partial Public Class Wfrm_ModificaSolicitud
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'session("usuario")
        If Not Page.IsPostBack Then
            txtCantidad.Attributes.Add("onkeypress", "javascript:return ValidNum(event);")
            txtNsolicitud.Attributes.Add("onkeypress", "javascript:return ValidNum(event);")
            'Session("usu") = Session("usuario")
            ViewState("usu") = Session("usuario")
            llena_combo()
            carga_bodega()
            FK_EMPRESA()

            CreaDatatable()
            Panel3.Visible = False
            Panel3.UpdateAfterCallBack = True
            Carga_Datos_Usuario()
            carga_Estado_solicitud()
            Cargar_Grupo()

        End If
    End Sub
    Sub Cargar_Grupo()
        'cmbGrupo
        Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence

        rp.StoredProcedure = "SP_OC_CARGA_GRUPO"
        'rp.AddParametro(ViewState("usu"))
        exec.ExecuteDataTable(rp)

        If rp.TieneDatos Then
            cmbGrupo.DataTextField = "tdescripcion"
            cmbGrupo.DataValueField = "ncodigo_pki"
            cmbGrupo.DataSource = rp.DtTable
            cmbGrupo.DataBind()
            cmbGrupo.UpdateAfterCallBack = True
        End If

    End Sub

    Sub CreaDatatable() 'JQ

        Dim Dt2 As New DataTable
        Dt2.Columns.Add("")
        Dt2.Columns.Add("nCodigo_PKI")
        Dt2.Columns.Add("Producto")
        Dt2.Columns.Add("tNombre")
        Dt2.Columns.Add("CANT_SOLICITADA")
        Dt2.Columns.Add("FK_PROVEEDOR_TEMP")
        Dt2.Columns.Add("PRECIO_TEMP")
        Dt2.Columns.Add("PROVEEDOR")
        ViewState("DT") = Dt2
    End Sub
    Sub FK_EMPRESA()
        Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence

        rp.StoredProcedure = ("SP_OC_GET_FK_EMPRESA")
        rp.AddParametro(ViewState("usu"))
        exec.ExecuteDataTable(rp)

        If rp.TieneDatos Then
            ViewState("FK_EMPRESA") = rp.DtTable.Rows(0).Item(0).ToString
        End If
        If rp.Errores Then
            'erro
        Else
            'ok
        End If

    End Sub

    Sub carga_bodega()
        Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence

        rp.StoredProcedure = ("SP_OC_CARGA_BODEGA")
        rp.AddParametro(ViewState("usu"))
        exec.ExecuteDataTable(rp)

        If rp.TieneDatos Then
            cmbBodega.DataTextField = "DESCRIPCION"
            cmbBodega.DataValueField = "ID_BODEGA"
            cmbBodega.DataSource = rp.DtTable
            cmbBodega.DataBind()
            cmbBodega.UpdateAfterCallBack = True
        End If
    End Sub

    Sub llena_combo()
        ' If ViewState("Vista") Is Nothing Then
        Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence

        If Rad_orden.SelectedValue = "tnombre1" Then
            rp.StoredProcedure = ("SP_OC_PRODUCTOS")
        Else
            rp.StoredProcedure = ("SP_OC_PRODUCTOS_cod")
        End If

        rp.AddParametro(cmbGrupo.SelectedValue)

        exec.ExecuteDataTable(rp)

        If rp.TieneDatos Then
            'Dim Vista As New DataView(rp.DtTable)
            'ViewState("Vista") = rp.DtTable
            'Vista.Sort = Me.Rad_orden.SelectedValue '& " desc"

            cmbProducto.DataTextField = "tnombre"
            cmbProducto.DataValueField = "nCodigo_PKI"
            cmbProducto.DataSource = rp.DtTable
            cmbProducto.DataBind()
            cmbProducto.UpdateAfterCallBack = True
        End If
        cmbProducto_SelectedIndexChanged(Nothing, Nothing)

    End Sub

    Private Function SumarProducto(ByVal codProducto As String) As Decimal 'JQ
        Dim i As Integer
        For i = 0 To DataGrid2.Items.Count - 1
            If codProducto = DataGrid2.Items(i).Cells(1).Text Then
                Return DirectCast(DirectCast(DataGrid2.Items(i).Cells(4).FindControl("Label5"), System.Web.UI.Control), Label).Text

            End If
        Next
        Return 0
    End Function

    Sub AgregaProducto(ByVal fk_solicitud As String, ByVal fk_producto As String, ByVal Cant_sol As Decimal, ByVal Proveedor As String, ByVal Precio As Decimal) 'JQ

        Dim exec As New DotNetDal.SQLPersistence(True, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence

        rp.StoredProcedure = "SP_OC_ADD_SOLICITUD_PRODUCTO"

        rp.AddParametro(fk_solicitud)
        rp.AddParametro(fk_producto)
        rp.AddParametro(Cant_sol)
        rp.AddParametro(ViewState("usu"))

        rp.AddParametro(Proveedor) '21-07-2011
        rp.AddParametro(Precio) '21-07-2011

        exec.ExecuteNonQuery(rp)
        exec.Commit()

        If rp.Errores Then

        End If
    End Sub
    Sub ActualizaProducto(ByVal fk_solicitud As String, ByVal fk_producto As String, ByVal Cant_sol As Decimal, ByVal Proveedor As String, ByVal Precio As Decimal) 'JQ

        Dim exec As New DotNetDal.SQLPersistence(True, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence

        rp.StoredProcedure = "SP_OC_UPDATE_SOLICITUD_PRODUCTO"

        rp.AddParametro(fk_solicitud)
        rp.AddParametro(fk_producto)
        rp.AddParametro(Cant_sol)
        rp.AddParametro(ViewState("usu"))

        rp.AddParametro(Proveedor) '21-07-2011
        rp.AddParametro(Precio) '21-07-2011

        exec.ExecuteNonQuery(rp)
        exec.Commit()

        If rp.Errores Then

        End If
    End Sub

    Sub GetProductosSolicitud()  'JQ      
        Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence
        rp.StoredProcedure = "SP_OC_GET_SOLICITUD_PRODUCTO"
        rp.AddParametro(txtSolicitud.Text)
        exec.ExecuteDataTable(rp)



        ViewState("DT") = rp.DtTable
        If rp.TieneDatos Then
            lbl_nro_productos.Text = DataGrid2.Items.Count
            lbl_nro_productos.UpdateAfterCallBack = True


            ViewState("datos") = rp.DtTable.Rows(0).Item(4).ToString
            DataGrid2.DataSource = rp.DtTable
            DataGrid2.DataBind()
            DataGrid2.UpdateAfterCallBack = True
        Else
            lbl_nro_productos.Text = 0
            lbl_nro_productos.UpdateAfterCallBack = True

            DataGrid2.DataSource = Nothing
            DataGrid2.DataBind()
            DataGrid2.UpdateAfterCallBack = True
        End If

    End Sub
    Protected Sub DataGrid2_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DataGrid2.SelectedIndexChanged

        If btnAgrega.Visible = False Then
            Exit Sub
        End If


        Dim exec As New DotNetDal.SQLPersistence(True, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence
        If Val(txtSolicitud.Text) = 0 Then
            Dim i As Integer
            For i = 0 To ViewState("DT").Rows.Count - 1
                If ViewState("DT").Rows(i).Item(1) = DataGrid2.SelectedItem.Cells(1).Text Then  'cmbProducto.SelectedValue Then DU
                    ViewState("DT").Rows(i).Delete()
                    Exit For
                End If
            Next

            DataGrid2.DataSource = ViewState("DT")
            DataGrid2.DataBind()
            DataGrid2.UpdateAfterCallBack = True

        Else
            'se quita fila directamente de la tabla

            For i = 0 To ViewState("DT").Rows.Count - 1
                If ViewState("DT").Rows(i).Item(0) = DataGrid2.SelectedItem.Cells(1).Text Then  'cmbProducto.SelectedValue Then DU
                    ViewState("DT").Rows(i).Delete()
                    Exit For
                End If
            Next

            'hacer sp pa eliminar de la tabla  --- parecido al update 

            rp.StoredProcedure = "SP_OC_DELETE_PRODUCTO"
            rp.AddParametro(DataGrid2.SelectedItem.Cells(1).Text)
            rp.AddParametro(DirectCast(DataGrid1.SelectedItem.Cells(0).Controls(1), Anthem.LinkButton).Text)
            'DataGrid1.SelectedItem.Cells(0).Text)
            exec.ExecuteNonQuery(rp)
            exec.Commit()
            If rp.Errores Then

            End If

            'Cargar grilla
            GetProductosSolicitud()

        End If

        Try
            lbl_nro_productos.Text = DataGrid2.Items.Count
            lbl_nro_productos.UpdateAfterCallBack = True
        Catch ex As Exception

        End Try

    End Sub

    Protected Sub DataGrid1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DataGrid1.SelectedIndexChanged
        txtSolicitud.Text = DirectCast(DataGrid1.SelectedItem.Cells(0).Controls(1), Anthem.LinkButton).Text
        txtSolicitud.UpdateAfterCallBack = True
        GetProductosSolicitud()
        '7 COSTO
        '6 OBSERVACION

        txt_Observacion.Text = DataGrid1.SelectedItem.Cells(6).Text.Replace("&nbsp;", "")
        txt_Observacion.UpdateAfterCallBack = True

        cmbCosto.SelectedValue = DataGrid1.SelectedItem.Cells(7).Text
        cmbCosto.UpdateAfterCallBack = True

        cmbBodega.SelectedValue = DataGrid1.SelectedItem.Cells(3).Text
        cmbBodega.UpdateAfterCallBack = True
        cmbBodega.Enabled = False
        cmbBodega.UpdateAfterCallBack = True

        pnl_productos.Visible = True
        Panel2.Visible = True
        btnFinSol.Visible = True
        btnGrabaSol.Visible = True
        lblSolicitud0.Visible = True
        btnImprimir.Visible = True
        btnImprimir.UpdateAfterCallBack = True

        lblSolicitud0.UpdateAfterCallBack = True
        pnl_productos.UpdateAfterCallBack = True
        Panel2.UpdateAfterCallBack = True
        btnFinSol.UpdateAfterCallBack = True
        btnGrabaSol.UpdateAfterCallBack = True

        btnAgrega.Visible = True
        btnAgrega.UpdateAfterCallBack = True


        lbl_nro_solicitud.Text = txtSolicitud.Text
        lbl_nro_solicitud.UpdateAfterCallBack = True

        Try
            lbl_nro_productos.Text = DataGrid2.Items.Count
            lbl_nro_productos.UpdateAfterCallBack = True
        Catch ex As Exception

        End Try

        If ViewState("datos") = "1" Then
            btnFinSol.Enabled = False
            btnFinSol.UpdateAfterCallBack = True

            btnAgrega.Enabled = False
            btnAgrega.UpdateAfterCallBack = True
            btnGrabaSol.Enabled = False
            btnGrabaSol.UpdateAfterCallBack = True
            txtCantidad.ReadOnly = True
            txtCantidad.UpdateAfterCallBack = True
            ViewState("datos") = Nothing
            DataGrid2.Enabled = False
            DataGrid2.UpdateAfterCallBack = True
            cmbBodega.Enabled = False
            cmbProducto.Enabled = False
            cmbBodega.UpdateAfterCallBack = True
            cmbProducto.UpdateAfterCallBack = True
        Else
            btnFinSol.Enabled = True
            btnFinSol.UpdateAfterCallBack = True

            btnAgrega.Enabled = True
            btnAgrega.UpdateAfterCallBack = True
            btnGrabaSol.Enabled = True
            btnGrabaSol.UpdateAfterCallBack = True
            txtCantidad.ReadOnly = False
            txtCantidad.UpdateAfterCallBack = True
            ViewState("datos") = Nothing
            DataGrid2.Enabled = True
            DataGrid2.UpdateAfterCallBack = True
            cmbBodega.Enabled = True
            cmbProducto.Enabled = True
            cmbBodega.UpdateAfterCallBack = True
            cmbProducto.UpdateAfterCallBack = True
        End If
    End Sub

    Protected Sub btnAgrega_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnAgrega.Click

        'Precio
        Dim Precio0 As String
        Try
            Precio0 = Cmb_Proveedor.SelectedItem.Text
        Catch ex As Exception
            Precio0 = "0"
        End Try

        Try
            txt_Precio.Text = Precio0.Split(" ")(1)
            txt_Precio.UpdateAfterCallBack = True
        Catch ex As Exception
            txt_Precio.Text = 0
            txt_Precio.UpdateAfterCallBack = True
        End Try

        'Validar Factor Empaque 11-09-2011
        If Not Valida_Factor_Empaque() Then
            Mensaje("El factor de empaque no permite esta cantidad")
            Exit Sub
        End If


        If txtCantidad.Text = "" Or txtCantidad.Text = "0" Then
            Mensaje("Wfrm_CreaSolicitud", "ValNumeroIng")
            Exit Sub

        End If

        If Not IsNumeric(txtCantidad.Text) Then
            Mensaje("Wfrm_CreaSolicitud", "ValNumero2")
            Exit Sub

        End If
      

        Dim Cant As Decimal = SumarProducto(cmbProducto.SelectedValue) 'JQ

        Dim FK_PROVEEDOR As String = Nothing

        If Cmb_Proveedor.SelectedValue.Split(" ")(0) <> "-1" Then
            FK_PROVEEDOR = Cmb_Proveedor.SelectedValue.Split(" ")(0)
        End If

        Dim Precio As Decimal = 0

        Try
            Precio = Val(txt_Precio.Text)
        Catch ex As Exception
            'no es un numero 
            Precio = 0
            Exit Sub
        End Try


        If Val(txtSolicitud.Text) = 0 Then
            'nueva solicitud, se maneja grilla por ViewState("DT")
            If Cant > 0 Then
                Dim i As Integer
                For i = 0 To ViewState("DT").Rows.Count - 1
                    If ViewState("DT").Rows(i).Item(1) = cmbProducto.SelectedValue Then
                        ViewState("DT").Rows(i).Item(4) = txtCantidad.Text + Cant
                        Exit For
                    End If
                Next
            Else
                'se agrega fila al datatable
                Dim dr As DataRow
                dr = ViewState("DT").NewRow
                dr(0) = ""
                dr(1) = cmbProducto.SelectedValue.ToString
                dr(2) = cmbProducto.SelectedItem.ToString
                dr(3) = cmbProducto.SelectedItem.ToString
                dr(4) = txtCantidad.Text
                ViewState("DT").Rows.Add(dr)

            End If

            DataGrid2.DataSource = ViewState("DT")
            DataGrid2.DataBind()
            DataGrid2.UpdateAfterCallBack = True

        Else
            'se agrega producto directamente a la tabla
            If Cant > 0 Then
                ActualizaProducto(Val(txtSolicitud.Text), cmbProducto.SelectedValue, txtCantidad.Text + Cant, FK_PROVEEDOR, Precio)
            Else
                AgregaProducto(Val(txtSolicitud.Text), cmbProducto.SelectedValue, txtCantidad.Text, FK_PROVEEDOR, Precio)
            End If
            'Se carga grilla nuevamente
            GetProductosSolicitud()
        End If


        txtCantidad.Text = ""
        txtCantidad.UpdateAfterCallBack = True

        Try
            lbl_nro_productos.Text = DataGrid2.Items.Count
            lbl_nro_productos.UpdateAfterCallBack = True
        Catch ex As Exception

        End Try

    End Sub

    Function Valida_Factor_Empaque() As Boolean
        'Por ejemplo si factor es 1 o null no se verifica..
        'Marcelino Barraza Arenas: si el factor es >= 2 si 
        'el resto de la division unidad/factor de enpaque es igual cero 
        'es valido sino lo rechaza

        'SP_OC_Get_Empaque
        ' @id_producto numeric(18,0)
        ',@proveedor numeric(18,0)

        Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence
        rp.StoredProcedure = "SP_OC_Get_Empaque"
        rp.AddParametro(cmbProducto.SelectedValue)
        rp.AddParametro(Cmb_Proveedor.SelectedValue.Split(" ")(0))
        rp.AddParametro(txt_Precio.Text)
        exec.ExecuteScalar(rp)
        If rp.TieneDatos Then
            If rp.Get_Value = 1 Then
                Return True
            Else
                If Val(txtCantidad.Text) Mod rp.Get_Value = 0 Then
                    Return True
                Else
                    Return False
                End If
            End If
        Else
            Return False
        End If


    End Function

    Protected Sub btnGrabaSol_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnGrabaSol.Click
        If DataGrid2.Items.Count = 0 Then
            Mensaje("Wfrm_CreaSolicitud", "GrillaVacia")
            Exit Sub

        End If
        Dim id_sol As String = grabar()
        lbl_nro_solicitud.Text = id_sol
        lbl_nro_solicitud.UpdateAfterCallBack = True
        If id_sol = 0 Then
            Mensaje("Wfrm_CreaSolicitud", "ErrorSol")
            txtSolicitud.Text = Nothing
            txtSolicitud.UpdateAfterCallBack = True
        Else
            btnImprimir.Visible = True
            btnImprimir.UpdateAfterCallBack = True
            btnBuscaSol_Click(Nothing, Nothing) ' 16 julio 2011
        End If
    End Sub
    Function grabar()
        Dim exec As New DotNetDal.SQLPersistence(True, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence
        Dim id_sol As String

        rp.ClearParametro()
        rp.StoredProcedure = "SP_OC_INSERT_SOLICITUD"
        rp.AddParametro(ViewState("FK_EMPRESA")) '("FK_EMPRESA") 'nClinicaID de usuario, HACER SP
        rp.AddParametro(cmbBodega.SelectedValue) '("FK_BODEGA") 'id_bodega del combobox
        rp.AddParametro(ViewState("usu")) '("FK_USUARIO_CREA") 'PONDRE Tcodigo_user_PK, FVOR REVISAR
        rp.AddParametro("0") '("ESTADO_SOL")
        rp.AddParametro(txt_Observacion.Text.Trim) '16 julio 2011
        rp.AddParametro(Val(txtSolicitud.Text)) '16 JULIO 2011
        rp.AddParametro(cmbCosto.SelectedValue) '23 JULIO 2011
        exec.ExecuteNonQuery(rp) 'cuando queremos que devuelva un valor, el @@IDENTITY del SP.
        'id_sol = rp.GetScalar
        'ViewState("id_sol") = rp.GetScalar

        'For i = 0 To DataGrid2.Items.Count - 1
        '    rp.ClearParametro()
        '    rp.TieneDatos = False

        '    rp.StoredProcedure = "SP_OC_INSERT_SOLICITUD_PRODUCTO"
        '    rp.AddParametro(id_sol)
        '    rp.AddParametro(DataGrid2.Items(i).Cells(1).Text.ToString)
        '    rp.AddParametro((DirectCast(DirectCast(DataGrid2.Items(i).Cells(4).FindControl("Label5"), System.Web.UI.Control), Label).Text))
        '    rp.AddParametro(ViewState("usu"))

        '    exec.ExecuteNonQuery(rp)

        'Next

        If Not rp.Errores Then
            exec.Commit()
            Return txtSolicitud.Text
        Else
            Mensaje(rp.MensajeError)
        End If
        Return 0

    End Function
    Sub Mensaje(ByVal Msg As String)

        If Anthem.Manager.IsCallBack Then
            Anthem.Manager.AddScriptForClientSideEval("alert('" & Msg & "')")
        Else
            Page.ClientScript.RegisterStartupScript(Me.GetType, "alertar", "<script>alert('" & Msg & "')</script>")
        End If

    End Sub
    Protected Sub btnFinSol_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnFinSol.Click
        Dim exec As New DotNetDal.SQLPersistence(True, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence

        If DataGrid2.Items.Count = 0 Then
            Mensaje("Wfrm_CreaSolicitud", "ValGri")
            Exit Sub

        End If

        Dim id_sol As String = grabar()
        lbl_nro_solicitud.Text = id_sol
        lbl_nro_solicitud.UpdateAfterCallBack = True

        If id_sol > 0 Then
            rp.StoredProcedure = ("SP_OC_UPDATE_SOLICITUD")
            rp.AddParametro(id_sol)
            exec.ExecuteNonQuery(rp)
            exec.Commit()
            btnBuscaSol_Click(Nothing, Nothing)
            If rp.Errores Then
                'error
            Else

                txtSolicitud.Text = Nothing
                txtSolicitud.UpdateAfterCallBack = True
                Mensaje("Solicitud Finalizada y Enviada Correctamente")


                btnFinSol.Visible = False
                btnFinSol.UpdateAfterCallBack = True

                btnGrabaSol.Visible = False
                btnGrabaSol.UpdateAfterCallBack = True

                btnAgrega.Visible = False
                btnGrabaSol.UpdateAfterCallBack = True

                ViewState("id_sol") = Nothing

                btnAgrega.Visible = False
                btnAgrega.UpdateAfterCallBack = True

            End If

        End If

        'btnGrabaSol_Click(Nothing, Nothing)


    End Sub

    Sub Mensaje(ByVal tbl As String, ByVal codigo As String)

        Dim mensaje As String = FuncionesGlobal.GetMensaje(tbl, codigo)


        If Anthem.Manager.IsCallBack Then
            Anthem.Manager.AddScriptForClientSideEval("alert('" & mensaje & "')")
        Else
            Page.ClientScript.RegisterStartupScript(Me.GetType, "alertar", "<script>alert('" & mensaje & "')</script>")
        End If


    End Sub
    Sub Mensaje0(ByVal tbl As String, ByVal codigo As String)

        Dim mensaje As String = FuncionesGlobal.GetMensaje(tbl, codigo)


        If Anthem.Manager.IsCallBack Then
            Anthem.Manager.AddScriptForClientSideEval("alert('" & mensaje & "')")
        Else
            Page.ClientScript.RegisterStartupScript(Me.GetType, "alertar", "<script>alert('" & mensaje & "')</script>")
        End If


    End Sub
    Sub Carga_Datos_Usuario()
        Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence
        rp.StoredProcedure = "SP_OC_Get_INFO_FORM"
        rp.AddParametro(ViewState("usu"))
        exec.ExecuteDataTable(rp)

        If rp.TieneDatos Then
            lbl_clinica.Text = rp.DtTable.Rows(0).Item("tNombreFantasia").ToString.Trim
            lbl_usuario.Text = rp.DtTable.Rows(0).Item("Tnombre").ToString.Trim
            lbl_fecha.Text = Now

            lbl_clinica.UpdateAfterCallBack = True
            lbl_usuario.UpdateAfterCallBack = True
            lbl_fecha.UpdateAfterCallBack = True

        End If

    End Sub
    Protected Sub btnBuscaSol_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnBuscaSol.Click
        BuscarSol()
        'Dim exec As New DotNetDal.SQLPersistence(True, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        'Dim rp As New DotNetResponse.SQLPersistence


        'If txtStartDate.Text = "" Or txtEndDate.Text = "" Or txtNsolicitud.Text <> "" Then

        '    If Not IsNumeric(txtNsolicitud.Text) Or txtNsolicitud.Text = "" Then
        '        Mensaje("Falta Ingresar Filtro de busqueda")
        '        Exit Sub

        '    End If
        '    rp.StoredProcedure = "SP_OC_CONSULTA_SOLICITUD_IDSOLICITUD"
        '    rp.AddParametro(txtNsolicitud.Text)
        '    rp.AddParametro(ViewState("usu"))
        '    exec.ExecuteDataTable(rp)
        '    If rp.TieneDatos Then
        '        Panel3.Visible = True
        '        Panel3.UpdateAfterCallBack = True
        '        DataGrid1.DataSource = rp.DtTable
        '        DataGrid1.DataBind()
        '        DataGrid1.UpdateAfterCallBack = True
        '        For i = 0 To DataGrid1.Items.Count - 1
        '            DataGrid1.Items(i).Cells(1).Text = DataGrid1.Items(i).Cells(1).Text.Substring(6, 2) & "-" & DataGrid1.Items(i).Cells(1).Text.Substring(4, 2) & "-" & DataGrid1.Items(i).Cells(1).Text.Substring(0, 4)  'Convert.ToDateTime(DataGrid1.Items(i).Cells(1).Text).ToString("dd-MM-yyyy")
        '            DataGrid1.UpdateAfterCallBack = True
        '        Next

        '    Else
        '        Mensaje(rp.MensajeError)
        '        DataGrid1.DataSource = Nothing
        '        DataGrid1.DataBind()
        '        DataGrid1.UpdateAfterCallBack = True
        '    End If
        'End If

        'If txtNsolicitud.Text = "" Then

        '    If txtStartDate.Text = "" Or txtEndDate.Text = "" Then
        '        Mensaje0("Wfrm_CreaSolicitud", "RangoFecha")
        '        Exit Sub

        '    End If

        '    Dim fecha_ini As String
        '    Dim fecha_fin As String
        '    fecha_ini = Convert.ToDateTime(txtStartDate.Text).ToString("yyyyMMdd")
        '    fecha_fin = Convert.ToDateTime(txtEndDate.Text).ToString("yyyyMMdd")

        '    If fecha_ini.ToString > fecha_fin.ToString Then

        '        Mensaje0("Wfrm_CreaSolicitud", "ValFecha")

        '    End If
        '    rp.StoredProcedure = "SP_OC_CONSULTA_SOLICITUD_FECHA"
        '    rp.AddParametro(fecha_ini)
        '    rp.AddParametro(fecha_fin)
        '    rp.AddParametro(ViewState("usu"))
        '    exec.ExecuteDataTable(rp)

        '    If rp.TieneDatos Then
        '        Panel3.Visible = True
        '        Panel3.UpdateAfterCallBack = True
        '        DataGrid1.DataSource = rp.DtTable
        '        DataGrid1.DataBind()
        '        DataGrid1.UpdateAfterCallBack = True

        '        For i = 0 To DataGrid1.Items.Count - 1
        '            DataGrid1.Items(i).Cells(1).Text = DataGrid1.Items(i).Cells(1).Text.Substring(6, 2) & "-" & DataGrid1.Items(i).Cells(1).Text.Substring(4, 2) & "-" & DataGrid1.Items(i).Cells(1).Text.Substring(0, 4)  'Convert.ToDateTime(DataGrid1.Items(i).Cells(1).Text).ToString("dd-MM-yyyy")
        '            DataGrid1.UpdateAfterCallBack = True
        '        Next

        '    Else
        '        Mensaje0("Wfrm_CreaSolicitud", "ValFecha")
        '        DataGrid1.DataSource = Nothing
        '        DataGrid1.DataBind()
        '        DataGrid1.UpdateAfterCallBack = True
        '    End If

        'End If



    End Sub

    Sub BuscarSol()
        Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence

        If txtStartDate.Text.Trim = "" And txtEndDate.Text.Trim = "" And txtNsolicitud.Text.Trim = "" And txtNsolicitud0.Text.Trim = "" Then
            Mensaje("Falta Ingresar filtro de busqueda")
            Exit Sub
        End If

        Dim fecha_ini As String
        Dim fecha_fin As String
        Dim nroi As String = Val(txtNsolicitud.Text.Trim)
        Dim nrot As String = Val(txtNsolicitud0.Text.Trim)
        Dim Estado As String = cmb_estado_solicitud.SelectedValue

        If cmb_estado_solicitud.SelectedValue = "-1" Then
            Estado = Nothing
        End If
        If Val(txtNsolicitud.Text.Trim) = 0 Then
            nroi = Nothing
        End If

        If Val(txtNsolicitud0.Text.Trim) = 0 Then
            nrot = Nothing
        End If

        If IsDate(txtStartDate.Text) Then
            fecha_ini = Convert.ToDateTime(txtStartDate.Text).ToString("yyyyMMdd")
        Else
            fecha_ini = Nothing
        End If
        If IsDate(txtEndDate.Text) Then
            fecha_fin = Convert.ToDateTime(txtEndDate.Text).ToString("yyyyMMdd")
        Else
            fecha_fin = Nothing
        End If


        rp.StoredProcedure = "SP_OC_GET_SOLICITUD"
        rp.AddParametro(nroi)
        rp.AddParametro(nrot)
        rp.AddParametro(fecha_ini)
        rp.AddParametro(fecha_fin)
        rp.AddParametro(Estado)
        rp.AddParametro(ViewState("usu"))

        exec.ExecuteDataTable(rp)


        If rp.TieneDatos Then
            Panel3.Visible = True
            Panel3.UpdateAfterCallBack = True
            DataGrid1.DataSource = rp.DtTable
            DataGrid1.DataBind()
            DataGrid1.UpdateAfterCallBack = True

            'For i = 0 To DataGrid1.Items.Count - 1
            '    DataGrid1.Items(i).Cells(2).Text = DataGrid1.Items(i).Cells(2).Text.Substring(6, 2) & "-" & DataGrid1.Items(i).Cells(2).Text.Substring(4, 2) & "-" & DataGrid1.Items(i).Cells(2).Text.Substring(0, 4)  'Convert.ToDateTime(DataGrid1.Items(i).Cells(1).Text).ToString("dd-MM-yyyy")
            '    DataGrid1.UpdateAfterCallBack = True
            'Next
        Else
            DataGrid1.DataSource = Nothing
            DataGrid1.DataBind()
            DataGrid1.UpdateAfterCallBack = True
        End If
        If rp.Errores Then
            Mensaje(rp.MensajeError)
        End If

        Panel2.Visible = True

    End Sub

    Sub carga_Estado_solicitud()
        Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence
        rp.StoredProcedure = "SP_OC_CARGA_ESTADO"
        exec.ExecuteDataTable(rp)

        If rp.TieneDatos Then
            cmb_estado_solicitud.DataTextField = "s_texto"
            cmb_estado_solicitud.DataValueField = "id_concepto"
            cmb_estado_solicitud.DataSource = rp.DtTable
            cmb_estado_solicitud.DataBind()

            cmb_estado_solicitud.SelectedValue = "-1"
            cmb_estado_solicitud.UpdateAfterCallBack = True

        End If
    End Sub

    Protected Sub Rad_orden_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Rad_orden.SelectedIndexChanged
        llena_combo()
    End Sub

    Protected Sub btnImprimir_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnImprimir.Click
        Dim rp As New Rpt_Solicitud
        Dim _CRConnectionInfo As ConnectionInfo

        _CRConnectionInfo = New ConnectionInfo()
        _CRConnectionInfo.ServerName = ConfigurationManager.AppSettings("cnn_server") '"application.dyndns-web.com"
        _CRConnectionInfo.DatabaseName = ConfigurationManager.AppSettings("cnn_database") '"comprasFarm"
        _CRConnectionInfo.UserID = ConfigurationManager.AppSettings("cnn_userid") '"lilas"
        _CRConnectionInfo.Password = ConfigurationManager.AppSettings("cnn_password") '"lilas"

        Dim tables As Tables
        tables = rp.Database.Tables
        For i = 0 To tables.Count - 1
            Dim tableLogonInfo As TableLogOnInfo = tables(i).LogOnInfo
            tableLogonInfo.ConnectionInfo = _CRConnectionInfo
            tables(i).ApplyLogOnInfo(tableLogonInfo)
        Next
        rp.SetParameterValue(1, lbl_nro_solicitud.Text)
        rp.SetParameterValue(0, ViewState("usuario"))
        rp.ExportToHttpResponse(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, Page.Response, True, "Solicitud_Nro_" & lbl_nro_solicitud.Text & ".pdf")

    End Sub

    Protected Sub cmbProducto_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cmbProducto.SelectedIndexChanged
        Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence
        rp.StoredProcedure = "Sp_OC_Get_UnidadMedida_Producto"
        rp.AddParametro(cmbProducto.SelectedValue)
        exec.ExecuteScalar(rp)
        lbl_unidad.Text = rp.GetScalar
        lbl_unidad.UpdateAfterCallBack = True

        '21 Julio 2011 - Presentar Proveedor y precio
        Cmb_Proveedor.DataSource = FuncionesGlobal.GetProveedorPrecioSOL(cmbProducto.SelectedValue)
        Cmb_Proveedor.DataValueField = "tRUT_PK"
        Cmb_Proveedor.DataTextField = "tNombres"
        Cmb_Proveedor.DataBind()
        Cmb_Proveedor.UpdateAfterCallBack = True

        Dim item As New ListItem
        item.Text = "Seleccione"
        item.Value = "-1"
        item.Selected = True
        Cmb_Proveedor.Items.Add(item)


        Cmb_Proveedor_SelectedIndexChanged1(Nothing, Nothing)

    End Sub

    Protected Sub Cmb_Proveedor_SelectedIndexChanged1(ByVal sender As Object, ByVal e As EventArgs) Handles Cmb_Proveedor.SelectedIndexChanged
        'Precio
        Dim Precio As String
        Try
            Precio = Cmb_Proveedor.SelectedItem.Text
        Catch ex As Exception
            Precio = "0"
        End Try

        Try
            txt_Precio.Text = Precio.Split(" ")(1)
            txt_Precio.UpdateAfterCallBack = True
        Catch ex As Exception
            txt_Precio.Text = 0
            txt_Precio.UpdateAfterCallBack = True
        End Try

    End Sub

    Protected Sub cmbGrupo_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cmbGrupo.SelectedIndexChanged
        llena_combo()
    End Sub
End Class